package com.kycxika.app.activity.pic;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.kycxika.app.PicCutoutActivity;
import com.kycxika.app.R;
import com.kycxika.app.activity.base.BaseActivity;
import com.kycxika.app.dbutils.IntImp.KycXkIApi;
import com.kycxika.app.dbutils.inter.HttpQuestResultListener;
import com.kycxika.app.utils.PreferenceHelper;
import com.kycxika.app.view.UpdateInfoDialog;

import java.util.ArrayList;
import java.util.List;

import static com.kycxika.app.KycXikaApplication.context;

public class SelfInfoActivity extends BaseActivity implements HttpQuestResultListener, View.OnClickListener {

    private KycXkIApi xkIApi;
    private View mTop;
    private View mBack;
    private TextView mTitle;
    private View mTitle_image;

    private TextView username_txt;

    private TextView nike_txt;

    private TextView gender_txt;

    private TextView phone_txt;
    private ImageView header_image;


    protected void initView() {
        mTop = findViewById(R.id.top);
        setTop(mTop);
        getTitle("个人信息");
        getVisibility(true, this);

        header_image = (ImageView) findViewById(R.id.header_image);
        username_txt = (TextView) findViewById(R.id.username_txt);
        nike_txt = (TextView) findViewById(R.id.nike_txt);
        gender_txt = (TextView) findViewById(R.id.gender_txt);

        phone_txt = (TextView) findViewById(R.id.phone_txt);


        nike_txt.setText(PreferenceHelper.getCurUserNick(this));
        username_txt.setText(PreferenceHelper.getCurUserName(this));
        gender_txt.setText(String.valueOf(PreferenceHelper.getCurUserSex(this)));

        xkIApi = KycXkIApi.getInstance(this);
        xkIApi.setHttpQuestResultListener(this);

        if(!TextUtils.isEmpty(xkIApi.getUserHeaderUrl())) {
            Bitmap bitmap = BitmapFactory.decodeFile(xkIApi.getUserHeaderUrl());
            header_image.setImageBitmap(bitmap);
        }else {
            header_image.setImageResource(R.drawable.ce_01);
        }
    }


    protected void initData() {

    }

    protected void initListener() {
        findViewById(R.id.header_image).setOnClickListener(this);
        findViewById(R.id.select_phone).setOnClickListener(this);
        findViewById(R.id.select_gender).setOnClickListener(this);
        findViewById(R.id.select_nike).setOnClickListener(this);
        findViewById(R.id.select_username).setOnClickListener(this);
        findViewById(R.id.select_img).setOnClickListener(this);
        username_txt.setOnClickListener(this);
        phone_txt.setOnClickListener(this);
        gender_txt.setOnClickListener(this);
        nike_txt.setOnClickListener(this);
    }

    @Override
    public int getLayout() {
        return R.layout.activity_self_info;
    }

    @Override
    public void onError(Object e) {

    }

    @Override
    public void onSuccess(Object object) {

    }

    @Override
    public void onQuestErroCode(Object object) {

    }

    private void choosePictrue() {
        Intent intent = new Intent(context, PhotoPickerActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean(PhotoPickerActivity.IS_MULTI_SELECT, false);
        bundle.putString(PhotoPickerActivity.SELECT_RESULTS, null);
        intent.putExtras(bundle);
        startActivityForResult(intent, 1001);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1001 && resultCode == RESULT_OK) {
            String resultPhotoUri = data.getStringExtra(PhotoPickerActivity.SELECT_RESULTS);
            if (resultPhotoUri == null) {
                return;
            }
            data.setClass(context, PicCutoutActivity.class);
            startActivityForResult(data, 1002);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.select_phone:
            case R.id.phone_txt:
                updateInfoDialog(phone_txt.getText().toString());
                break;
            case R.id.select_gender:
            case R.id.gender_txt:
                updateInfoDialog(gender_txt.getText().toString());
                break;
            case R.id.select_nike:
            case R.id.nike_txt:
                updateInfoDialog(nike_txt.getText().toString());
                break;
            case R.id.select_username:
            case R.id.username_txt:
                updateInfoDialog(username_txt.getText().toString());
                break;
            case R.id.select_img:
            case R.id.header_image:
                List<String> urlList = new ArrayList<>();
                boolean showEdit = false;
                if (!TextUtils.isEmpty(xkIApi.getUserAcount())) {  //如果是当前用户
                    showEdit = true;
                    String headerUrl = xkIApi.getUserHeaderUrl();
                    if (!TextUtils.isEmpty(headerUrl)) {
                        urlList.add(headerUrl);
                        UserHeadActivity.ImageSize imageSize = new UserHeadActivity.ImageSize(v.getMeasuredWidth(), v.getMeasuredHeight());
                        UserHeadActivity.startUserHeadActivity(context, urlList, 0, imageSize, showEdit);
                    } else {
                        //如果自己的头像是空，则需要选择头像
                        performCodeWithPermission("App请求访问相册权限", new PermissionCallback() {
                            @Override
                            public void hasPermission() {
                                choosePictrue();
                            }

                            @Override
                            public void noPermission() {
                                //Toasty.warning(context, "读取相册权限异常", Toast.LENGTH_SHORT, true).show();
                                Toast.makeText(SelfInfoActivity.this, "读取相册权限异常", Toast.LENGTH_SHORT).show();
                            }
                        }, READ_EXTERNAL_STORAGE_permission, WRITE_EXTERNAL_STORAGE_PERMISSION);

                    }
                } else {
                    urlList.add(xkIApi.getUserHeaderUrl());
                    UserHeadActivity.ImageSize imageSize = new UserHeadActivity.ImageSize(v.getMeasuredWidth(), v.getMeasuredHeight());
                    UserHeadActivity.startUserHeadActivity(context, urlList, 0, imageSize, showEdit);
                }
                break;
        }
    }

    private void updateInfoDialog(final String info) {
        final UpdateInfoDialog dialog = new UpdateInfoDialog(this);
        dialog.setEditTxt(info);
        dialog.setNoOnclickListener(new UpdateInfoDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                dialog.dismiss();
            }
        });
        dialog.setYesOnclickListener(new UpdateInfoDialog.onYesOnclickListener() {
            @Override
            public void onYesClick(String string) {
                if(string.equals(info)){
                    //不需要修改
                    dialog.dismiss();
                }else {

                }
            }
        });
        dialog.show();
    }
}
